import abc
from typing import Any, Callable, TypeVar, Optional, Any, Dict, Coroutine, Union
from dataclasses import dataclass


def id_generator():
    """id生成器"""
    counter = 0
    while True:
        yield counter
        counter += 1
        if counter > (1 << 30):
            counter = 0


ServiceAddress = TypeVar("ServiceAddress", str, bytes, Any)
ServiceInfo = TypeVar("ServiceInfo", str, bytes)


class Service(abc.ABC):
    @abc.abstractmethod
    def close(self, *args, **kwargs):
        ...

    @abc.abstractmethod
    def name(self):
        ...


class ServiceDiscover(abc.ABC):
    """服务发现抽象类"""

    @abc.abstractmethod
    def get_all_service(self, node_name: str):
        """获取所有可用服务"""
        ...

    @abc.abstractmethod
    def register_service(self, node_name: str, val: Any, *args, **kwargs):
        """注册一个服务"""
        ...

    @abc.abstractmethod
    def remove_service(self, key: str, *args, **kwargs):
        """销毁一个服务"""
        ...

    @abc.abstractmethod
    def watch(self, key: str, callback: Optional[Callable] = None):
        """服务监听"""
        ...

    @abc.abstractmethod
    def notice(self, node_name: str, service_info: ServiceInfo):
        """服务状态广播"""
        ...


@dataclass
class DiscoverOption(object):
    node_name: str  # 节点名
    instance: ServiceDiscover  # 实例
    heartbeat_time: int = 60  # 心跳时间
